package com.yy.job.core.dataprocess;

import java.util.Date;

import com.yy.job.common.enums.JobStatusEnum;
import com.yy.job.constant.JobConstant;
import com.yy.job.core.domain.JobDomain;
import com.yy.job.core.run.worker.ModuleWorker;
import com.yy.job.spring.YYSpringBeanUtils;

public abstract class AbsJobDataProcess {

	protected ModuleWorker moduleWorker;

	public AbsJobDataProcess(ModuleWorker moduleWorker) {
		this.moduleWorker = moduleWorker;
	}

	/**
	 * 锁定任务,将job状态修为L<br>
	 * 根据版本号加锁
	 * 
	 * @param job
	 * @return
	 */
	protected boolean lockJobByVersion(JobDomain job, int runCount) {
		// 根据版本号加锁
		int modifyCnt = YYSpringBeanUtils.getJobManager().lockJobByVersion(job,
			String.format(JobConstant.FORMAT_OPERATOR, this.moduleWorker.getModuleWorkerName(),runCount));
		if (modifyCnt == 1) {
			job.setJobStatus(JobStatusEnum.LOCK);
			return true;
		}
		return false;
	}

	/**
	 * 判断当前job的期望启动时间是否满足可以启动
	 * 
	 * @param job
	 * @return
	 */
	protected boolean checkExpectedStartDate(JobDomain job) {
		if (job.getExpectedStartTime() == null) {
			return true;
		}
		// 预期启动时间在后，则暂时不启动
		if (job.getExpectedStartTime().getTime() > new Date().getTime()) {
			return false;
		}
		return true;
	}

}
